#pragma once
#include <iostream>
#include <string>
#include <functional>
#include "logMessage.hpp"

void seveceIO(const int &fd)
{
    char buffer[1024];
    while (true)
    {
        ssize_t num = read(fd, buffer, sizeof(buffer) - 1); // 对于从网络流中读取数据，用到的也是read函数 留一个位置给/0
        if (num > 0)
        {
            // 将目前渡到的数据当作字符串，截至目前
            std::string recvmessage = buffer;
            buffer[0] = 0;
            std::cout << "recv message#"<< recvmessage << std::endl;

            std::string outbuffer = buffer;
            outbuffer += " server[echo]";

            write(fd, outbuffer.c_str(), outbuffer.size()); // 进行多路转接
        }
        else if (num == 0)
        {
            // 代表client退出
            // std::cout << "client quit!,me too" << std::endl;
            debugLog(DEBUG,"client quit!,me too");
            break;
        }
    }
    close(fd);
}

class CalTask
{
public:
    using func_t = std::function<void(int)>; // c++11里面的特定
public:
    CalTask(){}
    CalTask(int sock,func_t func) : _sock(sock), _callback(func)
    {
    }
    void operator()()
    {
         _callback(_sock);

    }
private:
    int _sock;
    func_t _callback;
};
